// pages/cargo/cargo.js
var app = getApp();
var long = 0,
    width = 0,
    height = 0,
    density = 0,
    nums = 0;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        density: 0, //容积密度
        pickUpPayedMoney: 0,
        pickUpNums: 0,
        totalWeight: 0,
        totalVolume: 0,
        itemDetail: '',
        index: 0,
        codeList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(app.globalData.driversOrders, "driversOrders")
        var itemDetail = JSON.parse(options.item),
            index = options.index;
        this.setData({
            itemDetail: itemDetail,
            id: itemDetail.id,
            order: itemDetail.orders.orderGoodInfosList[index],
            index: index
        })
        console.log(this.data.order, "orderorderorderorder")
        density = parseFloat(Number(this.data.order.volumeWeight) / (Number(this.data.order.volume))).toFixed(2)
        this.setData({
            density: density
        })
        long = this.data.order.long,
            width = this.data.order.width,
            height = this.data.order.height;
        nums = this.data.order.nums;
        if (itemDetail.orders.insuredCost > 0) {
            this.setData({
                insurance: true
            })
        }
        this.setData({
            itemDetail: app.globalData.driversOrders,
            id: itemDetail.id,
        })
        var pickUpNums = 0,
            totalWeight = 0,
            totalVolume = 0,
            pickUpPayedMoney = itemDetail.orders.totalFee;
        for (let i = 0; i < itemDetail.orders.orderGoodInfosList.length; i++) { //费用体积计算
            pickUpNums += itemDetail.orders.orderGoodInfosList[i].nums;
            // pickUpPayedMoney += itemDetail.orders.orderGoodInfosList[i].freight;
            totalWeight += Number(itemDetail.orders.orderGoodInfosList[i].volumeWeight);
            totalVolume += Number(itemDetail.orders.orderGoodInfosList[i].volume)
        }
        // if()
        var insured = parseFloat(pickUpPayedMoney * itemDetail.orders.insuranceRate).toFixed(2) //保险金额计算
        pickUpPayedMoney = parseFloat(Number(pickUpPayedMoney) + Number(insured)).toFixed(2)
        this.setData({
            pickUpNums: pickUpNums,
            pickUpPayedMoney: pickUpPayedMoney,
            totalWeight: totalWeight,
            totalVolume: totalVolume,
            insured: insured
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onShow: function() {
        var that = this
        app.getApi.request("/api/Orders/OrdersGoodsInfoCodes", {
            orders: { id: app.globalData.id },
            orderGoodInfos: { id: app.globalData.orderid }
        }, "POST", (res) => {

            if (res.data.responseCode == 1000) {
                that.setData({
                    codeList: res.data.obj.codeList
                })
            }

        }, (res) => {})
    },
    long(e) {

        long = e.detail.value
        console.log(long, "")
        this.total()
    },
    width(e) {
        width = e.detail.value
        this.total()
    },
    height(e) {
        height = e.detail.value
        this.total()
    },
    total() {
        var order = this.data.order;
        var volume = parseFloat((long * width * height * nums) * 0.000001).toFixed(6)
        var volumeWeight = parseFloat(density * volume).toFixed(2)
        order.long = long
        order.width = width
        order.height = height
        order.volumeWeight = volumeWeight
        order.volume = volume
        this.setData({
            order: order
        })
    },
    formSubmit(e) { //修改globalData接单的数据
        app.globalData.driversOrders.orders.orderGoodInfosList[this.data.index].long = Number(this.data.order.long)
        app.globalData.driversOrders.orders.orderGoodInfosList[this.data.index].width = Number(this.data.order.width)
        app.globalData.driversOrders.orders.orderGoodInfosList[this.data.index].height = Number(this.data.order.height)
        app.globalData.driversOrders.orders.orderGoodInfosList[this.data.index].volumeWeight = this.data.order.volumeWeight
        app.globalData.driversOrders.orders.orderGoodInfosList[this.data.index].volume = this.data.order.volume
        wx.navigateBack({
            delta: 1
        })
    },
    gathering(e) {
        var that = this
        var item = JSON.stringify(e.currentTarget.dataset.item)
        wx.navigateTo({
            url: "../logs/logs?item=" + item + "&index=" + that.data.index
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    receiving() {
        wx.navigateBack()
    }
})